﻿using System;
using System.Globalization;
using Pantry.Business;
using Pantry.Entity;

namespace Pantry.Web.Users
{
    public partial class EditSupportingMaterial : System.Web.UI.Page
    {
        private SupportingMaterialBus _supBus;
        private SupportingMaterial _sm;

        protected void Page_Load(object sender, EventArgs e)
        {
            _supBus = new SupportingMaterialBus();
            string materialId = Request.QueryString["materialId"];

            if(!IsPostBack)
            {
                if (materialId != null)
                {
                    _sm = _supBus.SelectSupportingMaterialById(Convert.ToInt32(materialId));
                    TextBoxUrlEdit.Text = _sm.Url;
                    TextBoxDescriptionEdit.Text = _sm.Description;
                    TextBoxAddedByEdit.Text = _sm.AddedBy;
                    TextBoxAddedDateEdit.Text = _sm.AddedDate.ToString(CultureInfo.InvariantCulture);
                    DropDownListTypeEdit.SelectedValue = _sm.Type;
                }
                else
                {
                    Response.Redirect("~/Users/ViewListOrganisation.aspx");
                }
            }
        }

        protected void ButtonInActiveClick(object sender, EventArgs e)
        {
            string materialId = Request.QueryString["materialId"];
            string orgId = Request.QueryString["orgId"];
            _supBus = new SupportingMaterialBus();
            bool result = _supBus.MarkActivated(false, Convert.ToInt32(materialId));
            if (result)
            {
                Response.Redirect("OrganisationDetails.aspx?id=" + orgId + "&action=update&#table_wTabs-5");
            }
        }

        protected void SaveSupportClick(object sender, EventArgs e)
        {
            int materialId = int.Parse(Request.QueryString["materialId"]);
            string orgId = Request.QueryString["orgId"];
            _supBus = new SupportingMaterialBus();
            _sm = new SupportingMaterial
                      {
                          Url = TextBoxUrlEdit.Text.Trim(),
                          Description = TextBoxDescriptionEdit.Text.Trim(),
                          Type = DropDownListTypeEdit.SelectedItem.Text,
                          Id = Convert.ToInt32(materialId)
                      };
            int i = _supBus.UpdateSupportingMaterial(_sm);
            Page.ClientScript.RegisterStartupScript(GetType(), "MyKey", i > 0
                                                        ? "showmessage('Update supporting material successfully !','SUCCESS','','false');"
                                                        : "showmessage('Cannot update supporting material !','ERROR','','false');",
                                                    true);
            Response.Redirect("OrganisationDetails.aspx?id=" + orgId + "&#table_wTabs-5");
        }

        protected void BackListOriganisationClick(object sender, EventArgs e)
        {
            string orgId = Request.QueryString["orgId"];
            Response.Redirect("OrganisationDetails.aspx?id=" + orgId + "&#table_wTabs-5");
        }
    }
}
